<?php

namespace zhtfrontend\models\datas;

use common\components\helpers\Fun;
use common\models\datas\Customer;
use common\models\datas\Student;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%check_count}}".
 *
 * @property integer $id
 * @property integer $mechanic_id
 * @property integer $goods_id
 * @property integer $goods_price_id
 * @property integer $teacher_id
 * @property integer $customer_id
 * @property integer $student_id
 * @property integer $class_id
 * @property integer $class_lesson_id
 * @property integer $check_id
 * @property integer $lesson
 * @property integer $class_time
 * @property integer $status
 * @property integer $check_type
 * @property integer $check_user
 * @property integer $created_at
 */
class CheckCount extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%check_count}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mechanic_id', 'goods_id', 'orders_item_id', 'orders_items_relation_id', 'teacher_id', 'customer_id', 'student_id', 'class_id', 'class_lesson_id', 'check_id', 'lesson', 'class_time', 'status', 'check_type', 'check_user', 'created_at','is_frequency'], 'integer'],
            [['price','settlement_price'],'number'],
            [['class_type'],'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'mechanic_id' => Yii::t('zhtfrontend', '机构id'),
            'goods_id' => Yii::t('zhtfrontend', '商品id'),
            'orders_item_id' => Yii::t('zhtfrontend', '订单商品id'),
            'orders_items_relation_id' => Yii::t('zhtfrontend', '订单商品价格id'),
            'teacher_id' => Yii::t('zhtfrontend', '老师id'),
            'customer_id' => Yii::t('zhtfrontend', '会员id'),
            'student_id' => Yii::t('zhtfrontend', '学生id'),
            'class_id' => Yii::t('zhtfrontend', '班级id'),
            'class_lesson_id' => Yii::t('zhtfrontend', '班级id'),
            'check_id' => Yii::t('zhtfrontend', '打卡id'),
            'lesson' => Yii::t('zhtfrontend', '课次'),
            'price' => Yii::t('zhtfrontend', '课次单价'),
            'settlement_price' => Yii::t('zhtfrontend', '课次结算价格'),
            'class_type' => Yii::t('zhtfrontend', '课次类型'),
            'class_time' => Yii::t('zhtfrontend', '上课时间'),
            'status' => Yii::t('zhtfrontend', '状态（0未结算1已结算）'),
            'check_type' => Yii::t('zhtfrontend', '打卡状态（0学生打卡1代打卡）'),
            'check_user' => Yii::t('zhtfrontend', '打卡人id 学生/代打卡'),
            'created_at' => Yii::t('zhtfrontend', '添加时间'),
            'is_frequency' => Yii::t('zhtfrontend', '是否不限课次 0 否 1 是'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['class_time'] = function () {
            return \Yii::$app->formatter->asDate($this->class_time);
        };
        $fields['class_type'] =function (){
            switch ($this->class_type){
                case 'appointment':
                    return Yii::t('zhtfrontend', '奖学金课');
                case 'consume':
                    return Yii::t('zhtfrontend', '消费课程');
            }
        };
        $fields['check_type'] =function (){
            switch ($this->check_type){
                case 0:
                    return Yii::t('zhtfrontend', '否');
                case 1:
                    return Yii::t('zhtfrontend', '是');
            }
        };
        return $fields;
    }
    public function extraFields(){
        return [
            'teacher_name' => function(){
                return isset($this->teacher->name)?$this->teacher->name:'';
            },
            'student'=>function(){
                return isset($this->student->name)?$this->student->name:'';
            },
            'course' =>function (){
                return isset($this->goods->name)?$this->goods->name:'';
            }
        ];
    }

    public function getMechanic(){
        return $this->hasOne(Mechanic::className(),['id'=>'mechanic_id']);
    }
    public function getTeacher(){
        return $this->hasOne(Teacher::className(),['id'=>'teacher_id']);
    }
    public function getGoods(){
        return $this->hasOne(Goods::className(),['id'=>'goods_id']);
    }
    public function getCustomer(){
        return $this->hasOne(Customer::className(),['id'=>'customer_id'])->where(['site_id'=>Fun::getSiteId()]);
    }
    public function getStudent(){
        return $this->hasOne(Student::className(),['id'=>'student_id'])->where(['site_id'=>Fun::getSiteId()]);
    }
    public function getCheckuser(){
        if($this->check_type == 0){
            return $this->hasOne(Student::className(),['id'=>'check_user'])->where(['site_id'=>Fun::getSiteId()]);
        }else{
            return $this->hasOne(Admin::className(),['id'=>'check_user']);
        }
    }


    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }
}
